package com.tjec.project.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.tjec.common.utils.StringUtils;
import com.tjec.framework.web.domain.TreeSelect;
import com.tjec.project.system.domain.BmsMdArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.project.system.mapper.BmsMdIndustryCategoryMapper;
import com.tjec.project.system.domain.BmsMdIndustryCategory;
import com.tjec.project.system.service.IBmsMdIndustryCategoryService;

/**
 * 行业类别Service业务层处理
 * 
 * @author cjj
 * @date 2020-06-24
 */
@Service
public class BmsMdIndustryCategoryServiceImpl implements IBmsMdIndustryCategoryService 
{
    @Autowired
    private BmsMdIndustryCategoryMapper bmsMdIndustryCategoryMapper;

    /**
     * 查询行业类别
     * 
     * @param guid 行业类别ID
     * @return 行业类别
     */
    @Override
    public BmsMdIndustryCategory selectBmsMdIndustryCategoryByGuid(String guid)
    {
        return bmsMdIndustryCategoryMapper.selectBmsMdIndustryCategoryByGuid(guid);
    }

    /**
     * 查询行业类别列表
     * 
     * @param bmsMdIndustryCategory 行业类别
     * @return 行业类别
     */
    @Override
    public List<BmsMdIndustryCategory> selectBmsMdIndustryCategoryList(BmsMdIndustryCategory bmsMdIndustryCategory)
    {
        return bmsMdIndustryCategoryMapper.selectBmsMdIndustryCategoryList(bmsMdIndustryCategory);
    }

    /**
     * 新增行业类别
     * 
     * @param bmsMdIndustryCategory 行业类别
     * @return 结果
     */
    @Override
    public int insertBmsMdIndustryCategory(BmsMdIndustryCategory bmsMdIndustryCategory)
    {
        return bmsMdIndustryCategoryMapper.insertBmsMdIndustryCategory(bmsMdIndustryCategory);
    }

    /**
     * 修改行业类别
     * 
     * @param bmsMdIndustryCategory 行业类别
     * @return 结果
     */
    @Override
    public int updateBmsMdIndustryCategory(BmsMdIndustryCategory bmsMdIndustryCategory)
    {
        return bmsMdIndustryCategoryMapper.updateBmsMdIndustryCategory(bmsMdIndustryCategory);
    }

    /**
     * 批量删除行业类别
     * 
     * @param guids 需要删除的行业类别ID
     * @return 结果
     */
    @Override
    public int deleteBmsMdIndustryCategoryByGuids(String[] guids)
    {
        return bmsMdIndustryCategoryMapper.deleteBmsMdIndustryCategoryByGuids(guids);
    }

    /**
     * 删除行业类别信息
     * 
     * @param guid 行业类别ID
     * @return 结果
     */
    @Override
    public int deleteBmsMdIndustryCategoryByGuid(String guid)
    {
        return bmsMdIndustryCategoryMapper.deleteBmsMdIndustryCategoryByGuid(guid);
    }

    /**
     * 查询行业类别(两层)
     * @param bmsMdIndustryCategory
     * @return
     */
    @Override
    public List<BmsMdIndustryCategory> selectBmsMdIndustryCategoryListTwo(BmsMdIndustryCategory bmsMdIndustryCategory) {
        return bmsMdIndustryCategoryMapper.selectBmsMdIndustryCategoryListTwo(bmsMdIndustryCategory);
    }

    @Override
    public List<BmsMdIndustryCategory> buildBmsMdIndustryCategoryTreeSelect(List<BmsMdIndustryCategory> list) {
        List<BmsMdIndustryCategory> returnList = new ArrayList<>();
        List<String> tempList = new ArrayList<String>();
        for (BmsMdIndustryCategory bmsMdArea : list) {
            tempList.add(bmsMdArea.getGuid());
        }
        for (Iterator<BmsMdIndustryCategory> iterator = list.iterator(); iterator.hasNext();) {
            BmsMdIndustryCategory bmsMdIndustryCategory = (BmsMdIndustryCategory) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(bmsMdIndustryCategory.getParentId())) {
                recursionFn(list, bmsMdIndustryCategory);
                returnList.add(bmsMdIndustryCategory);
            }
        }
        if (returnList.isEmpty()) {
            returnList = list;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildBmsMdIndustryCategorySelect(List<BmsMdIndustryCategory> list) {
        List<BmsMdIndustryCategory> expertTypeTrees = buildBmsMdIndustryCategoryTreeSelect(list);
        return expertTypeTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<BmsMdIndustryCategory> list, BmsMdIndustryCategory t) {
        // 得到子节点列表
        List<BmsMdIndustryCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BmsMdIndustryCategory tChild : childList) {
            if (hasChild(list, tChild)) {
                // 判断是否有子节点
                Iterator<BmsMdIndustryCategory> it = childList.iterator();
                while (it.hasNext()) {
                    BmsMdIndustryCategory n = (BmsMdIndustryCategory) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<BmsMdIndustryCategory> getChildList(List<BmsMdIndustryCategory> list, BmsMdIndustryCategory t) {
        List<BmsMdIndustryCategory> tlist = new ArrayList<>();
        Iterator<BmsMdIndustryCategory> it = list.iterator();
        while (it.hasNext()) {
            BmsMdIndustryCategory n = (BmsMdIndustryCategory) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && t.getGuid().equals(n.getParentId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BmsMdIndustryCategory> list, BmsMdIndustryCategory t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }

}
